<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>为数组的每个元素应用回调函数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-keys.html">« array_keys</a></li>
      <li style="float: right;"><a href="function.array-merge-recursive.html">array_merge_recursive »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>为数组的每个元素应用回调函数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-map" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_map</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_map</span> &mdash; <span class="dc-title">
     为数组的每个元素应用回调函数
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-map-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_map</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span><span class="type"></span></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">...$arrays</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_map()</strong></span> 返回一个 <span class="type">array</span>，包含将 <code class="parameter">array</code> 的相应值作为回调的参数顺序调用 
   <code class="parameter">callback</code> 后的结果（如果提供了更多数组，还会利用 <code class="parameter">arrays</code> 
   传入）。<code class="parameter">callback</code> 函数形参的数量必须匹配 <span class="function"><strong>array_map()</strong></span> 
   实参中数组的数量。多余的实参数组将会被忽略。如果提供的实参数组的数量不足，将抛出 <span class="classname"><a href="class.argumentcounterror.html" class="classname">ArgumentCountError</a></span>。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.array-map-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
       回调函数 <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>，应用到每个数组里的每个元素。
      </p>
      <p class="para">
       多个数组操作合并时，<code class="parameter">callback</code> 可以设置为 <strong><code>null</code></strong>。
       如果只提供了 <code class="parameter">array</code> 一个数组，
       <span class="methodname"><strong>array_map()</strong></span> 会返回输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       数组，遍历运行 <code class="parameter">callback</code> 函数。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">arrays</code></dt>

     <dd>

      <p class="para">
       额外的数组列表，每个都遍历运行 <code class="parameter">callback</code> 函数。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-map-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数组，包含将 <code class="parameter">array</code> 的相应值作为回调的参数调用 <code class="parameter">callback</code>
   函数后的结果（如果提供了更多数组，还会利用 <code class="parameter">arrays</code> 传入）。
  </p>
  <p class="para">
   当仅仅传入一个数组时，返回的数组会保留传入参数的键（key）。
   传入多个数组时，返回的数组键是按顺序的 integer。
  </p>
 </div>



 <div class="refsect1 changelog" id="refsect1-function.array-map-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.0.0</td>
 <td>
  如果 <code class="parameter">callback</code> 接受引用传递参数，该方法将会抛出
  <strong><code>E_WARNING</code></strong>。
 </td>
</tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-map-examples">
  <h3 class="title">范例</h3>
  <p class="para">
     <div class="example" id="example-4926">
      <p><strong>示例 #1 <span class="function"><strong>array_map()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">cube</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'cube'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>
       这使得 <var class="varname">$b</var> 成为：
      </p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 1
    [1] =&gt; 8
    [2] =&gt; 27
    [3] =&gt; 64
    [4] =&gt; 125
)
</pre></div>
      </div>
     </div>
  </p>
  <p class="para">
   <div class="example" id="example-4927">
    <p><strong>示例 #2 <span class="function"><strong>array_map()</strong></span> 使用匿名函数</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$func&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">int&nbsp;$value</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">)));<br /><br /></span><span style="color: #FF8000">//&nbsp;或者从&nbsp;PHP&nbsp;7.4.0&nbsp;起：<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 2
    [1] =&gt; 4
    [2] =&gt; 6
    [3] =&gt; 8
    [4] =&gt; 10
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4928">
    <p><strong>示例 #3 <span class="function"><strong>array_map()</strong></span>：使用更多的数组</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">show_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$m</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">string<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"The&nbsp;number&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$n</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;is&nbsp;called&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$m</span><span style="color: #007700">}</span><span style="color: #DD0000">&nbsp;in&nbsp;Spanish"</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">map_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$n</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$m</span><span style="color: #007700">):&nbsp;array<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">];<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'uno'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dos'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tres'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cuatro'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cinco'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'show_Spanish'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'map_Spanish'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
// 打印 $c
Array
(
    [0] =&gt; The number 1 is called uno in Spanish
    [1] =&gt; The number 2 is called dos in Spanish
    [2] =&gt; The number 3 is called tres in Spanish
    [3] =&gt; The number 4 is called cuatro in Spanish
    [4] =&gt; The number 5 is called cinco in Spanish
)

// 打印 $d
Array
(
    [0] =&gt; Array
        (
            [1] =&gt; uno
        )

    [1] =&gt; Array
        (
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [3] =&gt; tres
        )

    [3] =&gt; Array
        (
            [4] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [5] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   传入两个及以上的数组时，它们元素数量将会相同。因为回调函数会并行地处理相互对应的元素。
   如果几个数组的元素数量不一致：空元素会扩展短那个数组，直到长度和最长的数组一样。
  </p>
  <p class="para">
   此函数有个有趣的用法：传入 <strong><code>null</code></strong> 作为回调函数的名称，将创建多维数组（一个数组，内部包含数组。）
  </p>
  <p class="para">
   <div class="example" id="example-4929">
    <p><strong>示例 #4 多个数组的合并操作</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'four'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'five'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'uno'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dos'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tres'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cuatro'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cinco'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; 1
            [1] =&gt; one
            [2] =&gt; uno
        )

    [1] =&gt; Array
        (
            [0] =&gt; 2
            [1] =&gt; two
            [2] =&gt; dos
        )

    [2] =&gt; Array
        (
            [0] =&gt; 3
            [1] =&gt; three
            [2] =&gt; tres
        )

    [3] =&gt; Array
        (
            [0] =&gt; 4
            [1] =&gt; four
            [2] =&gt; cuatro
        )

    [4] =&gt; Array
        (
            [0] =&gt; 5
            [1] =&gt; five
            [2] =&gt; cinco
        )

)
</pre></div>
    </div>
   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-4930">
    <p><strong>示例 #5 
     仅有 <code class="parameter">array1</code> 时，<code class="parameter">callback</code> 设置为 <strong><code>null</code></strong>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  int(1)
  [1]=&gt;
  int(2)
  [2]=&gt;
  int(3)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4931">
    <p><strong>示例 #6 <span class="function"><strong>array_map()</strong></span> 键（key）是 string</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"stringkey"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"value"</span><span style="color: #007700">);<br />function&nbsp;</span><span style="color: #0000BB">cb1</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">$a</span><span style="color: #007700">];<br />}<br />function&nbsp;</span><span style="color: #0000BB">cb2</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'cb1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">'cb2'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [&quot;stringkey&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
array(1) {
  [&quot;stringkey&quot;]=&gt;
  string(5) &quot;value&quot;
}
array(1) {
  [0]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;value&quot;
    [1]=&gt;
    string(5) &quot;value&quot;
  }
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4932">
    <p><strong>示例 #7 <span class="function"><strong>array_map()</strong></span> - 关联数组</strong></p>
    <div class="example-contents"><p>
     虽然 <span class="function"><strong>array_map()</strong></span> 不能直接支持使用数组的键（key）作为输入，但可以使用
     <span class="function"><a href="function.array-keys.html" class="function">array_keys()</a></span> 进行模拟。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$arr&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'v1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'First&nbsp;release'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'v2'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Second&nbsp;release'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'v3'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Third&nbsp;release'</span><span style="color: #007700">,<br />];<br /><br /></span><span style="color: #FF8000">//&nbsp;注意：&nbsp;在&nbsp;7.4.0&nbsp;之前，使用较长的语法来代替匿名函数。<br /></span><span style="color: #0000BB">$callback&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$k</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$v</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">string&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$k</span><span style="color: #DD0000">&nbsp;was&nbsp;the&nbsp;</span><span style="color: #0000BB">$v</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$callback</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">array_keys</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">array_values</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  string(24) &quot;v1 was the First release&quot;
  [1]=&gt;
  string(25) &quot;v2 was the Second release&quot;
  [2]=&gt;
  string(24) &quot;v3 was the Third release&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-map-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-filter.html" class="function" rel="rdfs-seeAlso">array_filter()</a> - 使用回调函数过滤数组的元素</span></li>
    <li class="member"><span class="function"><a href="function.array-reduce.html" class="function" rel="rdfs-seeAlso">array_reduce()</a> - 用回调函数迭代地将数组简化为单一的值</span></li>
    <li class="member"><span class="function"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - 使用用户自定义函数对数组中的每个元素做回调处理</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>